//
//  BubbleItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/7/24.
//

import SwiftUI

struct MyBubbleItemView: View {
    @State var roleId: String
    @State var bean: BubbleMallDataBean
    let itemWidth: CGFloat
    let isSelected: Bool
    @StateObject var viewModel = MyBubbleViewModel()

    var body: some View {
        VStack {
            ZStack {
                BubbleChatView(str: "哈喽", bean: BubbleExt.getBubble(id: bean.id) ?? BubbleBean())
            }.frame(width: itemWidth,height: itemWidth)
                .background(.bgItem)
                .clipShape(RoundedRectangle(cornerRadius: 8))
            ZStack{
                Button(action: {
                    showFunctionPopup()
                }) {
                    Text("立即装扮")
                        .foregroundColor(themeManager.themeColor)
                        .font(.system(size: 12))
                        .padding(.horizontal,20)
                        .padding(.vertical,5)
                    
                }.background(themeManager.themeColor.opacity(0.1))
                    .cornerRadius(5)
                    .frame(maxWidth: .infinity, alignment: .center)
            }
        }.frame(width: itemWidth)
    }
    
    //功能弹窗
    func showFunctionPopup() {
        SwiftEntryKitWrapper.showBottomPopup { dismiss in
            BubbleChooseDialogView() { clickIndex in
                dismiss()
                switch clickIndex {
                case 0:
                    //聊天气泡
                    viewModel.doBubble(roleId: roleId, id: bean.id ?? "", isComment: false)
                    break
                case 1:
                    //评论气泡
                    viewModel.doBubble(roleId: roleId, id: bean.id ?? "", isComment: true)
                    break
                default:break
                }
            }
        }
    }
}

private let previewBean: BubbleMallDataBean = {
    let bean : BubbleMallDataBean = BubbleMallDataBean()
    bean.id = "30"
    return bean
}()

#Preview {
    MyBubbleItemView(roleId:"", bean: previewBean, itemWidth: 300, isSelected: true)
}
